package com.tools.payhelper.utils.permission;

import android.app.Dialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import android.support.v7.app.AlertDialog;
import android.view.View;

import com.tools.payhelper.R;


public class DialogUtil {
    private static BaseDialog mPermissionDialog;
    private static final String PACKAGE_URL_SCHEME = "package:";
    public static void showPermissionManagerDialog(final Context context, String str) {
        if (null == mPermissionDialog) {
            mPermissionDialog = new BaseDialog(context);
        }
        mPermissionDialog.setDefaultText(R.string.common_dialog_title,
                "请在 设置-应用管理-" + context.getString(R.string.app_name) + "-权限管理 (将" + str + "权限打开)",
                R.string.permission_refuse,
                R.string.permission_setting,
                R.string.permission_setting, true);
        mPermissionDialog.setOnlyTwoButton();
        mPermissionDialog.setBtnListener(new BaseDialog.OnBtnClickListener() {
            @Override
            public void leftClick(Dialog dialog, View view) {
                super.leftClick(dialog, view);
                dialog.dismiss();
            }

            @Override
            public void rigntClick(Dialog dialog, View view) {
                super.rigntClick(dialog, view);
                startAppSettings(context);
                dialog.dismiss();
            }
        });
        mPermissionDialog.show();

//        new AlertDialog.Builder(context).setTitle("获取" + str + "权限被禁用")
//                .setMessage("请在 设置-应用管理-" + context.getString(R.string.app_name) + "-权限管理 (将" + str + "权限打开)")
//                .setNegativeButton("取消", null)
//                .setPositiveButton("去设置", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
//                        intent.setData(Uri.parse("package:" + context.getPackageName()));
//                        context.startActivity(intent);
//                    }
//                })
//                .show();
    }
    // 启动应用的设置
    private static void startAppSettings(Context context) {
        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.parse(PACKAGE_URL_SCHEME + context.getPackageName()));
        intent.putExtra("cmp", "com.android.settings/.applications.InstalledAppDetails");
        intent.addCategory("android.intent.category.DEFAULT");
        context.startActivity(intent);
    }
    public static void showLocServiceDialog(final Context context) {
        new AlertDialog.Builder(context).setTitle("手机未开启位置服务")
                .setMessage("请在 设置-系统安全-位置信息 (将位置服务打开))")
                .setNegativeButton("取消", null)
                .setPositiveButton("去设置", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent();
                        intent.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        try {
                            context.startActivity(intent);
                        } catch (ActivityNotFoundException ex) {
                            intent.setAction(Settings.ACTION_SETTINGS);
                            try {
                                context.startActivity(intent);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                })
                .show();
    }
}
